package nku.csc364;

/**
 * 
 * Raj Akula
 * CSC 365 Assignment 5
 * 
 * This class extends the AbstractHashTable and implements Double Hashing for its
 * hashing algorithm.
 *
 */

public class DoubleHashingHashTable extends AbstractHashTable {
	

	public DoubleHashingHashTable(int size, double loadFactor) {
	
		super(size,loadFactor);
		
	}

	/**
	 * Insert based on the hash key
	 */
	public void insert(String key, Object data) {
		
		array[hash(key)]=data;
		
	}

	
	public Object find(String key) {

		int idx=hash(key);
		if (array[idx]==null  || !array[idx].equals(key)){
			return null;
		}else{
			return array[idx];
		}
	}

	
	public void delete(String key) {

		array[hash(key)]=null;
		
	}
	
	/**
	 * 
	 * Find the index of the array where to insert. 
	 * If the index	already has a value
	 *    if its the same key, replace the value
	 *    else find the next index. 
	 * 
	 */
	
	private int hash(String key) {
		
		int idx =binaryConcat(key) % array.length; 
		int retVal=-1;
		
		//If not null and the value is a different we need to probe to next
		int count=0;
		while (array[idx] != null &&
					((Comparable)(array[idx])).compareTo(key)!=0 
					&& count <=array.length) {
			
			
			int next = nextStep(idx, key);

			if (next >= array.length) { 

				idx = next - array.length;
			} else {
				idx = next;
			}
			count++;
		}
		retVal=idx;
		
		
		return retVal;
	}
	
	/**
	 * Calculates the size of the next jump.
	 */
	private int nextStep(int current, String key) {

		int dhash = 7 - (binaryConcat(key) % 7);
		return dhash + current;
	}

	
}
